#include "mls.h"
#include <stdlib.h> 
#include <unistd.h>
#include <stdio.h>

static int h;

void m_skip(int m, int n)
{
  m_ins( m, m_len(m), n );
}



void test_m_skip()
{
  printf("starting m_skip... ");

  static char* s="HelloWorld_X";
  int m = m_create(3,1);
  m_write( m,0, s, 10 );
  m_skip(m,1);
  m_put(m, s+11 );
  *(char *)mls(m,10) = s[10];
  m_put(m, s+12 );

  if( strncmp( mls(m,0), s, 12 ) != 0 )
    {
      printf("Error in m_skip: written %s\nExpected %s\n", (char*)mls(m,0), s );
    }
  else puts("1. ok");

  m_clear(m);
  m_bzero(m);
  m_skip(m,1);
  m_put(m,s+1);
  *(char*) mls(m,0) = 'B';
  printf("expected 'Be' == '%s'\n", (char*) mls(m,0) );

  m_free(m);
}



void teste_m_resize(void)
{
}


int main(void)
{


  trace_level = 1;
  int i=-1; int *d;

  if(! fork() ) { puts("*** Not init."); m_create(10,1); }
  sleep( 1 );
  m_init();

  test_m_skip();

  h = m_create( 10, sizeof (int) );
  if(! fork() ) { puts("\n*** handle ok, data is zero"); m_put(h, 0 ); }
  sleep( 1 );
  if(! fork() ) { puts("\n*** Handle error"); m_put(0,&h ); }
  sleep( 1 );
  m_put(h, &h );
  if(! fork() ) { puts("\n*** Index Out of Bounds"); mls(h,1); }
  sleep( 1 );

  m_next(h,&i,&d);
  m_next(h,&i,&d);

  if( !fork()) 
  { 
    puts("\n*** WARN still allocated"); 
    m_destruct(); 
    return 0;
  }
  sleep( 1 );
  m_free(h);

  if(! fork() ) { puts("\n*** double free"); m_free(h); }
  sleep( 1 );

  m_destruct();
  return 0;
}
